<!--vue模板-->
<template>
  <div>
    产品管理<br />
    <!--搜索-->
    <div class="toolbar">
      产品名称:
      <el-input
        v-model="product.name"
        placeholder="请输入"
        clearable
        style="width: 150px; margin-right: 10px"
      />
      &nbsp; 产品型号:
      <el-input
        v-model="product.type"
        placeholder="请输入"
        clearable
        style="width: 150px; margin-right: 10px"
      />
      &nbsp;产品状态：
      <el-select
        v-model="product.status"
        placeholder="请选择"
        style="width: 240px"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      &nbsp;
      <el-button type="primary" @click="init">查询</el-button>
      <el-button type="primary" @click="reset">重置</el-button>
    </div>
    <div>
      <span
        ><el-button type="primary" @click="save()">添加</el-button>
        <el-button type="primary">导入</el-button></span
      >
    </div>
    <!--表格数据-->
    <el-table :data="tableData" style="width: 100%" stripe border>
      <el-table-column
        prop="code"
        label="产品编号"
        width="200"
        header-align="center"
        align="center"
      />
      <el-table-column
        prop="name"
        label="产品名称"
        width="200"
        header-align="center"
        align="center"
      />
      <el-table-column
        prop="type"
        label="产品分类"
        width="200"
        header-align="center"
        align="center"
      />
      <el-table-column
        prop="spec"
        label="型号"
        width="180"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="productNumber"
        label="产品图号"
        width="180"
        header-align="center"
        align="center"
      />
      <el-table-column
        prop="inventory"
        label="库存"
        width="180"
        header-align="center"
        align="center"
      />
      <el-table-column
        prop="createUser"
        label="创建人"
        width="180"
        header-align="center"
        align="center"
      />
      <el-table-column
        prop="status"
        label="状态"
        width="180"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="chg(row.id, row.status)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        header-align="center"
        align="center"
        width="180"
      >
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            title="编辑"
            @click="save(row)"
          />

          <el-button
            type="danger"
            :icon="Delete"
            circle
            title="删除"
            @click="del(row.id)"
          />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div style="height: 60px; line-height: 60px; background-color: #fff">
      <el-pagination
        background
        layout="sizes,total,prev, pager, next,jumper"
        :total="total"
        style="float: right; margin-right: 20px"
        @current-change="chpg"
        @size-change="chsize"
      />
    </div>
  </div>
</template>

<!--vue 组件选项定义-->
<script setup>
import { ref, onBeforeMount } from "vue";
import { queryProduct, deleteProduct, updateStatus } from "@/api/product.js";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { Delete, Edit } from "@element-plus/icons-vue";
const value = ref("");
const addProduct = ref(null);
const options = [
  {
    value: "1",
    label: "开启"
  },
  {
    value: "0",
    label: "未开启"
  }
];

//表格数据
const tableData = ref([]);

//获取路由实例
const router = useRouter();
//分页参数
const page = ref(1); //当前页数
const pageSize = ref(10); //每页记录数s
const total = ref(0); //总记录数

//搜索 条件
const product = ref({
  name: "",
  type: "",
  status: ""
});

//重置
function reset() {
  product.value = {
    name: "",
    type: "",
    status: ""
  };
  init();
}

//删除
function del(id) {
  ElMessageBox.confirm("你确定要删除吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      //确认删除
      todel(id);
    })
    .catch(() => {});
}

async function todel(id) {
  let res = await deleteProduct(id);
  if (res.code == 200) {
    ElMessage({
      message: "删除成功",
      type: "success"
    });
    init(); //重新查询数据
  }
}

//新增
function save(data) {
  isShow.value = true;
  addProduct.value.init(data);
}

async function chg(id, status) {
  //console.log(id, "    ", status);
  let res = await updateStatus(id, status);
  if (res.code == 200) {
    ElMessage({
      message: "修改成功",
      type: "success"
    });
    init(); //重新查询数据
  }
}

//方法
async function init() {
  let data = {
    page: page.value,
    pageSize: pageSize.value,
    name: product.value.name,
    type: product.value.type,
    status: product.value.status
  };
  
  
  let res = await queryProduct(data);
  console.log("res>>>>>",res);
  
  if (res.code == 200) {
   tableData.value = res.data.list;
    page.value = data.page;
    pageSize.value =data.pageSize;
    total.value = res.data.total;
  }
}


//改变当前页
function chpg(pg) {
  page.value = pg;
  init();
}
//改变每页记录数
function chsize(pgsize) {
  pageSize.value = pgsize;
  page.value = 1;
  init();
}
//生命周期钩子
onBeforeMount(() => {
  init();
});
</script>

<!--vue样式 scoped当前样式只适用于当前组件-->
<style scoped>
.toolbar {
  height: 60px;
  line-height: 60px;
  background-color: #fff;
  padding-left: 20px;
}
</style>
